<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" pageEncoding="UTF-8"%>
<head>
<title>ExtText</title>
<meta name="menu" content="mrMenu"/>
<meta name="nav" content="0"/>
<%@ include file="/common/extjs.jsp"%>
<script type="text/javascript" >
Ext.onReady(function(){

  Ext.QuickTips.init();
  var fm = Ext.form;
 var expander = new Ext.ux.grid.RowExpander({
 		expand:true,
        tpl : new Ext.XTemplate(
            '<p><b>消息内容:</b>&nbsp; {messageContent}</p><tpl if="sendFile"><p><b>附件下载:</b>&nbsp; <a href="'+appConfig.contPath +'/servlet/fileMsgDownload?tableId={id}">{sendFile}</a></p></tpl>'
        ),
        getRowClass : function(record, rowIndex, p, ds){
        p.cols = p.cols-1;
        var content = this.bodyContent[record.id];
        if(!content && !this.lazyRender){
            content = this.getBodyContent(record, rowIndex);
        }
        if(content){
            p.body = content;
        }
        var stat = this.state[record.id] ? 'x-grid3-row-expanded' : 'x-grid3-row-collapsed';
         var recFlag = record.get("recFlag");
               if(recFlag === "0") {
                  return 'x-grid3-row-unread ' +stat;
              } else {
            
              	return stat;
              }
    }
      });
  var cm = new Ext.grid.ColumnModel({
        columns:[
		  expander,
          new Ext.grid.RowNumberer(),
         
	      {
	    		header:'消息类型',
	    	 	dataIndex: 'messageType',
	    	 	hidden:true,
	        width: 80
	    	},
	    	{
	    		header:'主题',
	    	 	dataIndex: 'subObject',
	            width: 70,
	            renderer:Jinyi.project.Format.textAreaRenderer
	    	},

	      {
	    		header:'发送人',
	    	 	dataIndex: 'sendUser',
	        	width: 65
	    	},
	
	      {
	    		header:'发送时间',
	    	 	dataIndex: 'sendDate',
	        width: 75,
	        renderer: Jinyi.project.Format.formatDateRenderer
	    	},
	    		      {
	    		header:'加急',
	    	 	dataIndex: 'grade',
	        	width: 30,
  				renderer:function(value, metaData, record, rowIndex, colIndex, store) {
			      if (value == '1') {
			          metaData.css = 'row-massages-urgent';
			           return "加急";
			       } else {
			      	return "";
			      }
			  
			   }
			},
			{
	    		header:'id',
	    	 	dataIndex: 'id',
	    	 	hidden: true,
	            width: 80
	    		}],
    	  defaults : {
    		    align : 'center',
            sortable: true    
        }
    });

  /**
    *{name: '', type: 'date',dateFormat:'Y年m月d日'},
    */
  AppMessageInfo = Ext.data.Record.create([
    								{name: 'messageType', type: 'string'},
    								{name: 'subObject', type: 'string'},
    								{name: 'messageContent', type: 'string'},
    								{name: 'sendUser', type: 'string'},
    								{name: 'receiveUser', type: 'string'},
    								{name: 'sendDate', type: 'date',dateFormat:'Y年m月d日'},
    								{name: 'delFlag', type: 'string'},
    								{name: 'recFlag', type: 'string'},
    								{name: 'grade', type: 'string'},
    								{name: 'createDate', type: 'string'},
    								{name: 'remark', type: 'string'},
    								{name: 'sendFile', type: 'string'},
            				{name: 'id', type: 'string'}
      ]);
    // create the Data Store
    var store = new Ext.data.Store({
        			url: appConfig.contPath +'/system/appMessageInfos.html',
							baseParams :{
				 			method: 'searchExt',
				 			flag:'${param.flag}'
					},
        reader: new Ext.data.JsonReader({
        		 	totalProperty: 'totalCounts',
             	root: 'results',
             	id: 'id'
           }, AppMessageInfo)
    });
   // create the editor grid
   var grid = new Ext.grid.GridPanel({
   	    title:'',
        store: store,
        cm: cm,
        sm:	new Ext.grid.RowSelectionModel({singleSelect:true}),
        //view :
        frame:true,
        plugins:[expander],
        viewConfig: {
        forceFit: true,
        enableRowBody:true,
        showPreview:true,
        getRowClass : function(record, rowIndex, p, store){
        }
    	},
        clicksToEdit:1,
        loadMask:{msg :'数据加载中...'}, 
        stateful:true,       
        stateId:'cookieAppMessageInfo', 
        //页面渲染之后加载store
        listeners: {
	        	render: function(g) {
	     	       reloadData();
	        	},
	        	rowclick:function(g,rowindex) {
				    var recordInfo = g.getStore().getAt(rowindex);  // Get the Record
				    var dataId= recordInfo.get("id");
				    var recFlag= recordInfo.get("recFlag");
				    if(recFlag === "1") return ;
				    if(!dataId){return ; }	
				   	// 数据库操作
				   	Ext.Ajax.request({
			        url: appConfig.contPath +'/system/saveAppMessageInfo.html',
			        params:{
			         		id: dataId,
			         		method:"readMessage",
			    			mode:'json'
			        },
			        success: function(response, options){
			        	 try {
								var responseData = Ext.decode(response.responseText);
			           			if (responseData.success == true) {
			              			recordInfo.set("recFlag",'1');
			              			store.commitChanges();
			            		 } else {}
			            } catch (e) {}
			        },
			        failure: function(response, options){Ext.example.msg('警告!', '服务端发生错误,');}
			       }); 
			  },
				        	delay: 10 // Allow rows to be rendered.
       		}
	   		
    });

 	var viewport = new Ext.Viewport({
    layout: 'fit',
    items: [grid]
    });
/**
刷新纪录
*/
function reloadData(){
	store.removeAll();
	store.commitChanges();
	store.load({callback :function (r){
	 for (var i = 0, len = r.length; i < len; i++) {
            expander.toggleRow(i);
        }	
	}});
};

 /**
 * 删除一条记录
 */
 function deleteOneData (){
  var rid=grid.selModel.selections.keys; 
   	if(rid ==''||rid==null){
 	 	Ext.Msg.show({title: '没有选择项',msg: '请至少选择一项',modal: true,
                	 icon: Ext.Msg.INFO,buttons: Ext.Msg.OK}); return ;}
    Ext.Msg.confirm('删除操作', '删除将无法恢复,是否继续?', function(btn, text){
     if (btn == 'yes'){              	 
	    var recordInfo = store.getById(rid);
	    var dataId= recordInfo.get("id");
	    if(!dataId){store.remove(recordInfo); Ext.example.msg('信息', '已经删除该记录！'); return ; }	
	   	// 数据库操作
	   	Ext.Ajax.request({
        url: appConfig.contPath +'/system/deleteAppMessageInfo.html',
        params:{
         	id: dataId,
    			mode:'json'
        },
        success: function(response, options){
        	 try {
					var responseData = Ext.decode(response.responseText);
           			if (responseData.success == true) {
              			store.remove(recordInfo);
              			store.commitChanges();
           	    	 Ext.example.msg('信息', '数据删除成功！');  
            		 } else { Ext.example.msg('提示', '删除失败！');}
            } catch (e) { Ext.example.msg('提示', '删除失败！请稍后再尝试');}
        },
        failure: function(response, options){Ext.example.msg('警告!', '服务端发生错误,数据删除失败!');}
       }); 
      }
		}); 			 
 };

 //  加载完毕
	});
</script>

</head>
<body>

</body>	